package org.nfIt.controller;

import org.nfIt.entity.Result;
import org.nfIt.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * ClossName:FileUploadController
 * Pakage:org.nfIt.controller
 * Description
 *
 * @Author L
 * @Create 2024/6/6 19:18
 * @Version 2023.1.3
 */
@RestController
public class FileUploadController {

//    /**
//     * 存储到本地磁盘
//     * @param file
//     * @return
//     * @throws IOException
//     */
//    @PostMapping("/upload")
//    public Result<String> upload(MultipartFile file) throws IOException {
//        //获取原始文件名
//        String originalFilename = file.getOriginalFilename();
//        //使用UUid生成唯一文件名,从最后一个 . (点) 开始截
//        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        //把文件内容存储到本地磁盘
//        file.transferTo(new File("E:\\黑马程序员资料\\file\\" + fileName));
//        return Result.success("url 服务地址...");
//    }


    /**
     * 存储到阿里云
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //使用UUid生成唯一文件名,从最后一个 . (点) 开始截
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //把文件内容存储到本地磁盘
        String url  =  AliOssUtil.oploadFile(fileName,file.getInputStream());
        return Result.success(url);
    }

}
